package com.arsn.myf.system.controller;

import com.arsn.myf.common.util.result.Func;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.system.entity.Dept;
import com.arsn.myf.system.service.DeptService;
import com.arsn.myf.system.vo.DeptVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wildfly.common.annotation.NotNull;
import java.util.Arrays;

/**
 * 部门
 * @author zhongrj
 * @since 2022-07-10
 */
@RestController
@RequestMapping("/dept")
@AllArgsConstructor
@Api(value = "部门",tags = "部门")
public class DeptController {

    private final DeptService deptService;

    /**
     * 查询部门的分页数据
     * @param page
     * @param dept
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询部门的分页数据")
    public R list(Page<Dept> page, Dept dept){
        return R.data(deptService.page(page,new QueryWrapper<>(dept)));
    }


    /**
     * 自定义查询部门的分页数据
     * @param page
     * @param dept
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "自定义查询部门的分页数据")
    public R page(Page<DeptVO> page, DeptVO dept){
        return R.data(deptService.selectPageDeptList(page,dept));
    }


    /**
     * 部门详情信息
     * @param dept
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "部门详情信息")
    public R detail(Dept dept){
        return R.data(deptService.getOne(new QueryWrapper<>(dept)));
    }


    /**
     * 部门新增
     * @param dept
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "部门新增")
    public R save(@RequestBody Dept dept){
        return R.status(deptService.save(dept));
    }

    /**
     * 部门修改
     * @param dept
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "部门修改")
    public R update(@RequestBody Dept dept){
        return R.status(deptService.updateById(dept));
    }


    /**
     * 删除,批量删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除,批量删除")
    public R remove(@NotNull String ids){
        return R.status(deptService.removeByIds(Func.stringToLong(Arrays.asList(ids.split(",")))));
    }

    /**
     * 查询部门树
     * @param dept
     * @return
     */
    @GetMapping("/getDeptTree")
    @ApiOperation(value = "查询部门树")
    public R getDeptTree(DeptVO dept){
        return R.data(deptService.getDeptTree(dept));
    }

    /**
     * 部门懒加载列表
     * @param dept 部门参数实体
     * @return
     */
    @GetMapping("/getDeptLazyList")
    @ApiOperation(value = "部门懒加载列表")
    public R getDeptLazyList(DeptVO dept){
        return R.data(deptService.getDeptLazyList(dept));
    }
}
